﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
   
    /// </summary>
    public partial class MainWindow : Window
    {
        private string[] _selectedFiles;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "文本文件|*.txt|所有文件|*.*",
                Multiselect = true, // 允许多选
                Title = "选择两个文本文件"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                _selectedFiles = openFileDialog.FileNames;
                txtSelectedFiles.Text = $"已选择文件：\n{string.Join("\n", _selectedFiles)}";
            }
        }

        private void MergeFiles_Click(object sender, RoutedEventArgs e)
        {
            if (_selectedFiles == null || _selectedFiles.Length != 2)
            {
                MessageBox.Show("请先选择两个文件！");
                return;
            }

            try
            {
                // 读取文件内容
                string content1 = File.ReadAllText(_selectedFiles[0]);
                string content2 = File.ReadAllText(_selectedFiles[1]);
                string mergedContent = content1 + "\n\n" + content2;

                // 创建Data目录
                string dataDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                
                if(!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }
                // 生成新文件路径
                string newFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string newFilePath = System.IO.Path.Combine(dataDir, newFileName);

                // 写入文件
                File.WriteAllText(newFilePath, mergedContent);
                MessageBox.Show($"合并成功！文件已保存至：\n{newFilePath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}");
            }
        }
    }
}
